#!/bin/bash

###########################################################################
# Install Script - Domino Container Script                                #
# Version 1.0   15.01.2021                                                #
#                                                                         #
# (C) Copyright Daniel Nashed/NashCom 2021                                #
# Feedback domino_unix@nashcom.de                                         #
#                                                                         #
# Licensed under the Apache License, Version 2.0 (the "License");         #
# you may not use this file except in compliance with the License.        #
# You may obtain a copy of the License at                                 #
#                                                                         #
#      http://www.apache.org/licenses/LICENSE-2.0                         #
#                                                                         #
# Unless required by applicable law or agreed to in writing, software     #
# distributed under the License is distributed on an "AS IS" BASIS,       #
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.#
# See the License for the specific language governing permissions and     #
# limitations under the License.                                          #
###########################################################################

SCRIPT_NAME=$0
PARAM1=$1
SCRIPT_REAL=`realpath $0`
SCRIPT_DIR=`dirname $SCRIPT_REAL`

CONTAINER_SYSTEMD_NAME=domino_container.service
CONTAINER_INSTALL_CONFIG_FILE=domino_container
CONTAINER_INSTALL_CONFIG_FILEPATH=/etc/sysconfig/$CONTAINER_INSTALL_CONFIG_FILE

# if you really really need to, change this and all files will be patched

if [ -z "$DOMINO_START_SCRIPT_DIR" ]; then
  DOMINO_START_SCRIPT_DIR=/opt/nashcom/startscript
fi

ROOT_USER=root
ROOT_GROUP=root

#DOMINO_DEBUG_MODE="yes"

START_SCRIPT_DIR=`dirname $0`

if [ "$START_SCRIPT_DIR" = "/" ]; then
  START_SCRIPT_DIR=""
fi

if [ ! $EUID = "0" ]; then
  echo "Terminating installation - Please run as root user!"
  exit 1
fi

echo "CONTAINER_INSTALL_CONFIG_FILEPATH: [$CONTAINER_INSTALL_CONFIG_FILEPATH]"

header()
{
  echo
  echo "$@"
  echo
}

DebugText()
{
  if [ "$DOMINO_DEBUG_MODE" = "yes" ]; then

    if [ -z "$DOMINO_DEBUG_FILE" ]; then
      echo "Debug: $@"
    else
      echo "Debug: $@" >> $DOMINO_DEBUG_FILE 
    fi
  fi 

  return 0
}

check_file_busy()
{
  if [ ! -e "$1" ]; then
    return 0
  fi

  local TARGET_REAL_BIN=`readlink -f $1`
  local FOUND_TARGETS=`lsof "$TARGET_REAL_BIN" 2>/dev/null | grep "$TARGET_REAL_BIN"`

  if [ -n "$FOUND_TARGETS" ]; then
    return 1
  else
    return 0
  fi
}

nsh_cmp()
{
  if [ -z "$1" ]; then
    return 1
  fi

  if [ -z "$2" ]; then
    return 1
  fi

  if [ ! -e "$1" ]; then
    return 1
  fi

  if [ ! -e "$2" ]; then
    return 1
  fi

  if [ -x /usr/bin/cmp ]; then
    cmp -s "$1" "$2"
    return $?
  fi

  HASH1=`sha256sum "$1" | cut -d" " -f1`
  HASH2=`sha256sum "$2" | cut -d" " -f1`

  if [ "$HASH1" = "$HASH2" ]; then
    return 0
  fi

  return 1
}

install_file()
{
  SOURCE_FILE=$1
  TARGET_FILE=$2
  OWNER=$3
  GROUP=$4
  PERMS=$5

  if [ ! -r "$SOURCE_FILE" ]; then
    echo "[$SOURCE_FILE] Can not read source file"
    return 1
  fi

  if [ -e "$TARGET_FILE" ]; then

    nsh_cmp "$SOURCE_FILE" "$TARGET_FILE"
    if [ $? -eq 0 ]; then
      echo "[$TARGET_FILE] File did not change -- No update needed"
      return 0
    fi

    if [ ! -w "$TARGET_FILE" ]; then
      echo "[$TARGET_FILE] Can not update binary -- No write permissions"
      return 1
    fi

    check_file_busy "$TARGET_FILE"

    if [ $? -eq 1 ]; then
      echo "[$TARGET_FILE] Error - Can not update file -- Binary in use"
      return 1
    fi
  fi
  
  cp -f "$SOURCE_FILE" "$TARGET_FILE"
 
  if [ ! -z "$OWNER" ]; then
    chown $OWNER:$GROUP "$TARGET_FILE"
  fi

  if [ ! -z "$PERMS" ]; then
    chmod "$PERMS" "$TARGET_FILE"
  fi

  echo "[$TARGET_FILE] installed"

  return 2
}

install_file_noupdate()
{
  SOURCE_FILE=$1
  TARGET_FILE=$2
	
  if [ -e "$TARGET_FILE" ]; then
    echo "[$TARGET_FILE] Skipping file -- Not overwriting"  
    return 0
  fi
 
  install_file "$1" "$2" "$3" "$4" "$5" 
}


header "Installing Domino Container Script & Config"

if [ ! -e "$DOMINO_START_SCRIPT_DIR" ]; then
  mkdir -p -m 755 "$DOMINO_START_SCRIPT_DIR"
fi

DebugText "START_SCRIPT_DIR: [$START_SCRIPT_DIR]"

# create /etc/sysconfig if not present
if [ ! -e "/etc/sysconfig" ]; then
  mkdir -m 755 /etc/sysconfig
fi

install_file $START_SCRIPT_DIR/domino_container $DOMINO_START_SCRIPT_DIR/domino_container $ROOT_USER $ROOT_GROUP 755
install_file $START_SCRIPT_DIR/software.txt $DOMINO_START_SCRIPT_DIR/software.txt $ROOT_USER $ROOT_GROUP 755
install_file "$START_SCRIPT_DIR/extra/nshinfo.sh" "$DOMINO_START_SCRIPT_DIR/nshinfo.sh" $ROOT_USER $ROOT_GROUP 755

if [ ! -e /usr/bin/domino_container ]; then
  ln -s $DOMINO_START_SCRIPT_DIR/domino_container /usr/bin/domino_container
fi

cp -r $START_SCRIPT_DIR/build_image $DOMINO_START_SCRIPT_DIR
find $DOMINO_START_SCRIPT_DIR -name "*.sh" -exec chmod 755 {}  \;

if [ "$PARAM1" = "upd" ]; then
  install_file $START_SCRIPT_DIR/sysconfig/domino_container "$CONTAINER_INSTALL_CONFIG_FILEPATH" "$ROOT_USER" "$ROOT_GROUP" 644
  install_file $START_SCRIPT_DIR/sysconfig/env_container_domino11 "/etc/sysconfig/env_container_domino11" "$ROOT_USER" "$ROOT_GROUP" 644
  install_file $START_SCRIPT_DIR/sysconfig/env_container_domino12 "/etc/sysconfig/env_container_domino12" "$ROOT_USER" "$ROOT_GROUP" 644
else
  install_file_noupdate $START_SCRIPT_DIR/sysconfig/domino_container "$CONTAINER_INSTALL_CONFIG_FILEPATH" "$ROOT_USER" "$ROOT_GROUP" 644
  install_file_noupdate $START_SCRIPT_DIR/sysconfig/env_container_domino11 "/etc/sysconfig/env_container_domino11" "$ROOT_USER" "$ROOT_GROUP" 644
  install_file_noupdate $START_SCRIPT_DIR/sysconfig/env_container_domino12 "/etc/sysconfig/env_container_domino12" "$ROOT_USER" "$ROOT_GROUP" 644
fi

if [ -x /usr/bin/podman ]; then
  install_file_noupdate $START_SCRIPT_DIR/domino_container.service /etc/systemd/system/$CONTAINER_SYSTEMD_NAME $ROOT_USER $ROOT_GROUP 644
fi

if [ $? -eq 2 ]; then
  systemctl daemon-reload
fi

header Done

exit 0

